package ${packageName}.service.impl;

import ch.qos.logback.core.util.StringUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${packageName}.common.PageResponse;
import ${packageName}.common.Query;
import ${packageName}.common.SuperUtil;
import ${packageName}.exception.CustomException;
import ${packageName}.mapper.${mapperName};
import ${packageName}.model.dto.${dtoName};
import ${packageName}.model.en.${enName};
import ${packageName}.model.vo.${voName};
import ${packageName}.service.${serviceName};
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author 19324
*/
@Service
public class ${serviceName}Impl  extends ServiceImpl<${mapperName}, ${enName}>
        implements ${enName}Service {

    @Resource
    private ${mapperName} ${mapperName?uncap_first};

    //1.新增${enName}
    @Override
    public void create(${dtoName} ${dtoName?uncap_first}) {
        //1.获取参数
<#if columns??>
<#list vos as column>
<#if column.type='VARCHAR'||column.type='TEXT'||column.type='CHAR'>
        String ${column.propertyName?uncap_first}= ${dtoName?uncap_first}.get${column.propertyName?cap_first}();
</#if>
<#if column.type='INT'>
        Integer ${column.propertyName?uncap_first}= ${dtoName?uncap_first}.get${column.propertyName?cap_first}();
</#if>
<#if column.type='DATETIME'>
        Date ${column.propertyName?uncap_first}= ${dtoName?uncap_first}.get${column.propertyName?cap_first}();
</#if>
<#if column.type='BIGINT'>
        Long ${column.propertyName?uncap_first}= ${dtoName?uncap_first}.get${column.propertyName?cap_first}();
</#if>
<#if column.type='DOUBLE'>
        Double ${column.propertyName?uncap_first}= ${dtoName?uncap_first}.get${column.propertyName?cap_first}();
</#if>
<#if column.type='BIT'>
        Boolean ${column.propertyName?uncap_first}= ${dtoName?uncap_first}.get${column.propertyName?cap_first}();
</#if>
</#list>
</#if>
        //2.判断是否存在
        QueryWrapper<${enName}> queryWrapper = new QueryWrapper<>();
<#if columns??>
    <#list vos as column>
        queryWrapper.eq("${column.columnName}" ,${column.propertyName?uncap_first});
    </#list>
</#if>
        ${enName} ${enName?uncap_first} = ${mapperName?uncap_first}.selectOne(queryWrapper);
        if (${enName?uncap_first} != null) {
            throw new CustomException("数据已存在");
        }
        //3.封装对象
        ${enName?uncap_first} = new ${enName}();
<#if columns??>
    <#list vos as column>
        ${enName?uncap_first}.set${column.propertyName?cap_first}(${column.propertyName?uncap_first});
    </#list>
</#if>
        ${mapperName?uncap_first}.insert(${enName?uncap_first});
    }


    //2.查询By${enName}Id
    @Override
    public ${voName} queryBy${enName}Id(${dtoName} ${dtoName?uncap_first}) {
        //1.获取参数
        Long ${enName?uncap_first}Id = ${dtoName?uncap_first}.get${enName}Id();
        if(${enName?uncap_first}Id == null){
            throw new CustomException("查询参数不能为空");
        }
        //2.查询
        ${enName} ${enName?uncap_first} = ${mapperName?uncap_first}.selectById(${enName?uncap_first}Id);
        if(${enName?uncap_first} == null){
            throw new CustomException("数据不存在");
        }
        //3.封装对象
        return BeanUtil.copyProperties(${enName?uncap_first}, ${voName}.class);
    }

    //3.查询By其他字段
    @Override
    public ${voName} queryByOther(${dtoName} ${dtoName?uncap_first}) {
        //1.获取参数
        List<${query_name}> queryList = ${dtoName?uncap_first}.getQueryList();
            if(queryList == null){
                throw new CustomException("查询参数不能为空");
            }
        //2.查询
        Map<${stringName}, Object> queryMap = SuperUtil.listToMap(queryList);
        QueryWrapper<${enName}> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(queryMap);
        ${enName} ${enName?uncap_first}= ${mapperName?uncap_first}.selectOne(queryWrapper);
        if(${enName?uncap_first} == null){
            throw new CustomException("数据不存在");
        }
        //3.封装对象
        return BeanUtil.copyProperties(${enName?uncap_first}, ${voName}.class);
    }
    //4.查询所有
    @Override
    public List<${voName}> queryAll() {
        //1.查询
        List<${enName}> ${enName}s = ${mapperName?uncap_first}.selectList(null);
        // 2. 封装对象
        return ${enName}s.stream().map(${enName} -> BeanUtil.copyProperties(${enName}, ${voName}.class)).collect(Collectors.toList());
    }


    //5.删除By${enName}Id
    @Override
    public void deleteBy${enName}Id(${dtoName} ${dtoName?uncap_first}) {
        //1.获取参数
        Long ${enName?uncap_first}Id = ${dtoName?uncap_first}.getId();
        if(${enName?uncap_first}Id == null){
            throw new CustomException("删除参数不能为空");
        }
        //2.删除
        ${mapperName?uncap_first}.deleteById(${enName?uncap_first}Id);
        if(${mapperName?uncap_first}.selectById(${enName?uncap_first}Id) != null){
            throw new CustomException("删除失败");
        }
    }


    //6.修改By${enName}Id
    @Override
    public void updateBy${enName}Id(${dtoName} ${dtoName?uncap_first}) {
        //1.获取ID
        Long ${enName?uncap_first}Id = ${dtoName?uncap_first}.getId();
        //2.获取参数
<#if columns??>
<#list vos as column>
    <#if column.type='VARCHAR'||column.type='TEXT'||column.type='CHAR'>
        String ${column.propertyName?uncap_first}= ${dtoName?uncap_first}.get${column.propertyName?cap_first}();
    </#if>
    <#if column.type='INT'>
        Integer ${column.propertyName?uncap_first}= ${dtoName?uncap_first}.get${column.propertyName?cap_first}();
    </#if>
    <#if column.type='DATETIME'>
        Date ${column.propertyName?uncap_first}= ${dtoName?uncap_first}.get${column.propertyName?cap_first}();
    </#if>
    <#if column?? && column.type = 'BIGINT' && (column.isPrimaryKey?? &&!column.isPrimaryKey)>
        <#if dtoName?? && column.propertyName??>
        Long ${column.propertyName?uncap_first}= ${dtoName}.get${column.propertyName?cap_first}();
        </#if>
    </#if>
    <#if column.type='DOUBLE'>
        Double ${column.propertyName?uncap_first}= ${dtoName?uncap_first}.get${column.propertyName?cap_first}();
    </#if>
    <#if column.type='BIT'>
        Boolean ${column.propertyName?uncap_first}= ${dtoName?uncap_first}.get${column.propertyName?cap_first}();
    </#if>
</#list>
</#if>
        if(${enName?uncap_first}Id == null){
            throw new CustomException("修改参数不能为空");
        }
        //3.修改
        LambdaUpdateWrapper<${enName}> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(${enName}::getId, ${enName?uncap_first}Id);
    <#if columns??>
        <#list vos as column>
            <#if column.type='VARCHAR'||column.type='TEXT'||column.type='CHAR'>
        lambdaUpdateWrapper.set(StringUtil.isNullOrEmpty(${column.propertyName?uncap_first}),${enName}::get${column.propertyName?cap_first} ,${column.propertyName?uncap_first});
            </#if>
            <#if (column.type='INT' || column.type='BIGINT' || column.type='DOUBLE' || column.type='BIT')&&(column.isPrimaryKey?? &&!column.isPrimaryKey)>
        lambdaUpdateWrapper.set(${column.propertyName?uncap_first}!=null,${enName}::get${column.propertyName?cap_first} ,${column.propertyName?uncap_first});
            </#if>
        </#list>
    </#if>
        ${mapperName?uncap_first}.update(null, lambdaUpdateWrapper);
    }

    @Override
    public PageResponse<${voName}> query${enName}ByPage(${dtoName} ${dtoName?uncap_first}) {
        //1.获取参数
        String sortBy = ${dtoName?uncap_first}.getSortBy();
        Boolean isAsc = ${dtoName?uncap_first}.getIsAsc();
        Page<${enName}> page = ${dtoName?uncap_first}.toPageDefaultSortByUpdateTime();
        if(StrUtil.isNotBlank(sortBy)&& isAsc != null){
            page = ${dtoName?uncap_first}.toPage(sortBy, isAsc);
        }else if(StrUtil.isNotBlank(sortBy)){
            page = ${dtoName?uncap_first}.toPageWithSortBy(sortBy);
        }else if(isAsc != null){
            page = ${dtoName?uncap_first}.toPageWithIsAsc(isAsc);
        }

        List<${query_name}> queryList = ${dtoName?uncap_first}.getQueryList();
        List<${stringName}> serachList = ${dtoName?uncap_first}.getSearchList();
        //2.查询
        QueryWrapper<${enName}> queryWrapper = new QueryWrapper<>();
        if(queryList != null && !queryList.isEmpty()){
            Map<${stringName}, Object> queryMap = SuperUtil.listToMap(queryList);
            queryWrapper.allEq(queryMap);
        }

        // 处理搜索字段
        if (serachList != null && !serachList.isEmpty()) {
            String searchName = ${dtoName?uncap_first}.getSearchName(); // 假设 ${dtoName?uncap_first} 中有 searchValue 字段
            queryWrapper.and(wrapper -> {
                for (String field : serachList) {
                    wrapper.or().like(field, searchName);
                }
            });
        }
        List<${enName}> ${enName?uncap_first}s = ${mapperName?uncap_first}.selectList(queryWrapper);
        //3.封装
        List<${voName}> ${enName?uncap_first}Vos = ${enName?uncap_first}s.stream().map(${enName?uncap_first} -> BeanUtil.copyProperties(${enName?uncap_first}, ${voName}.class)).collect(Collectors.toList());
        return PageResponse.of(page,${enName?uncap_first}Vos);
    }

}